<?php
declare(strict_types=1);
/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://gitee.com/xmo/MineAdmin
 */

namespace Plugin\Attractions\Commerce\Repository\Commerce_address;

use Plugin\Attractions\Commerce\Model\Commerce_address\CommerceAddress;
use Hyperf\Database\Model\Builder;
use App\Repository\IRepository;
use Hyperf\Collection\Arr;

/**
 * 快递地址信息 Repository类
 */
class CommerceAddressRepository extends IRepository
{
   public function __construct(
        protected readonly CommerceAddress $model
    ) {}

    /**
     * 搜索处理器
     * @param Builder $query
     * @param array $params
     * @return Builder
     */
    public function handleSearch(Builder $query, array $params): Builder
    {
        
        // 主键ID
        if (isset($params['id']) && !empty($params['id'])) {
            $query->where('id', '=', $params['id']);
        }

        // 所属用户ID
        if (isset($params['user_id']) && !empty($params['user_id'])) {
            $query->where('user_id', '=', $params['user_id']);
        }

        // 姓名
        if (isset($params['name']) && !empty($params['name'])) {
            $query->where('name', 'like', '%'.$params['name'].'%');
        }

        // 手机号码
        if (isset($params['phone']) && !empty($params['phone'])) {
            $query->where('phone', 'like', '%'.$params['phone'].'%');
        }

        // 省
        if (isset($params['province']) && !empty($params['province'])) {
            $query->where('province', 'like', '%'.$params['province'].'%');
        }

        // 市
        if (isset($params['city']) && !empty($params['city'])) {
            $query->where('city', 'like', '%'.$params['city'].'%');
        }

        // 区
        if (isset($params['district']) && !empty($params['district'])) {
            $query->where('district', 'like', '%'.$params['district'].'%');
        }

        // 地址
        if (isset($params['address']) && !empty($params['address'])) {
            $query->where('address', 'like', '%'.$params['address'].'%');
        }

        // 是否默认地址：1-否，2-是
        if (isset($params['is_default']) && !empty($params['is_default'])) {
            $query->where('is_default', '=', $params['is_default']);
        }

        // 
        if (isset($params['created_at']) && !empty($params['created_at']) && is_array($params['created_at']) && count($params['created_at']) == 2) {
            $query->whereBetween(
                'created_at',
                [ $params['created_at'][0], $params['created_at'][1] ]
            );
        }

        // 
        if (isset($params['updated_at']) && !empty($params['updated_at']) && is_array($params['updated_at']) && count($params['updated_at']) == 2) {
            $query->whereBetween(
                'updated_at',
                [ $params['updated_at'][0], $params['updated_at'][1] ]
            );
        }

        // 
        if (isset($params['deleted_at']) && !empty($params['deleted_at']) && is_array($params['deleted_at']) && count($params['deleted_at']) == 2) {
            $query->whereBetween(
                'deleted_at',
                [ $params['deleted_at'][0], $params['deleted_at'][1] ]
            );
        }

        return $query;
    }
}